package com.code9xs.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.code9xs.mapper.*;
import com.code9xs.pojo.Association;
import com.code9xs.pojo.User;
import com.code9xs.pojo.UserRole;
import com.code9xs.service.ManageService;
import com.code9xs.service.UserService;
import com.code9xs.util.DateUtils;
import com.code9xs.util.PageUtil;
import com.code9xs.util.ReplaceHtml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Suhd
 * @date 2021/5/24
 * @time 12:15
 * @wechat i9xswanan
 * @github https://github.com/Code9xs
 */
@Service
@Transactional
public class ManageServiceImpl  implements ManageService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    AssociationMapper associationMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Override
    public PageUtil getAllUserInfo(Integer page,Integer pageSize) {
        PageUtil pageUtil = new PageUtil();
        int total = userMapper.selectAllUserCount();
        pageUtil.setTotal(total);
        pageUtil.setPage(page);
        pageUtil.setPageSize(pageSize);
        List<Map<String, Object>> userList = userMapper.selectAllUserPagination(pageUtil.getStartTotal() - 1, pageSize);
        userList.forEach(v -> {
            List<Map<String, Object>> roles = roleMapper.selectUserRoleByUid((Integer) v.get("id"));
            v.put("roles",roles);
        });
        pageUtil.setData(userList);
        return pageUtil;
    }

    @Override
    public Boolean modifyUserInfo(User user, JSONArray rolesArray) {
        // 如果修改用户id是根管理员，删除失败
        if (user.getId() == 123654){
            return false;
        }
        // 如果修改用户id是当前用户，删除失败
        if (user.getId() == userService.getCurrentUser().getId()){
            return false;
        }
        int flag = userMapper.updateByPrimaryKeySelective(user);
        if (rolesArray.size() > 0){
            // 删除用户原有权限
            userRoleMapper.deleteUserAllRole(user.getId());
            for (int i = 0;i < rolesArray.size();i++){
                UserRole userRole = new UserRole();
                userRole.setRid(rolesArray.getInteger(i));
                userRole.setUid(user.getId());
                userRoleMapper.insert(userRole);
            }
        }

        if (flag > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(Integer id) {
        int flag = userMapper.deleteByPrimaryKey(id);
        // 如果删除用户id是根管理员，删除失败
        if (id == 123654){
            return false;
        }
        // 如果删除用户id是当前用户，删除失败
        if (id == userService.getCurrentUser().getId()){
            return false;
        }
        if (flag > 0){
            return true;
        }
        return false;
    }


    @Override
    public boolean modifyAssociation(Association association) {
        final int flag = associationMapper.updateByPrimaryKeySelective(association);
        if (flag > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageUtil getAllAssociation(Integer page, Integer pageSize) {
        PageUtil pageUtil = new PageUtil();
        int total = associationMapper.selectAllAssociationCount();
        pageUtil.setTotal(total);
        pageUtil.setPage(page);
        pageUtil.setPageSize(pageSize);
        List<Map<String, Object>> maps = associationMapper.selectAllAssociationPagination(pageUtil.getStartTotal() - 1, pageSize);
        maps.forEach(k ->{
            String time = DateUtils.format((Date) k.get("time"), DateUtils.DATE_PATTERN);
            k.put("time",time);
        });
        pageUtil.setData(maps);
        return pageUtil;
    }

    @Override
    public boolean deleteAssociation(Integer id) {
        final int flag = associationMapper.deleteByPrimaryKey(id);
        if (flag > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageUtil getAllArticle(Integer page, Integer pageSize) {
        PageUtil pageUtil = new PageUtil();
        final int total = articleMapper.selectAllArticleCount();
        pageUtil.setPage(page);
        pageUtil.setPageSize(pageSize);
        pageUtil.setTotal(total);
        final List<Map<String, Object>> data = articleMapper.selectAllArticlePagination(pageUtil.getStartTotal() - 1, pageSize);
        data.forEach(v -> {
            v.put("time",DateUtils.format((Date) v.get("time"),DateUtils.DATE_TIME_PATTERN));
            v.put("noHtmlText",ReplaceHtml.delHTMLTag((String) v.get("text")).substring(0,10) + "...");
        });
        pageUtil.setData(data);
        return pageUtil;
    }

    @Override
    public boolean deleteArticle(Integer id) {
        int flag = articleMapper.deleteByPrimaryKey(id);
        if (flag > 0 ){
            return true;
        }
        return false;
    }
}
